<template>
  <div>
    <Header @searchList="searchList" />
    <div class="main">
      <el-row>
        <div class="btnBox">
          <el-button class="btn" @click="addTask">
            <i class="el-icon-circle-plus-outline" />
            新建
          </el-button>
          <el-button class="configBtn" @click="configDialog">
            工单配置
          </el-button>
        </div>
      </el-row>
      <el-table empty-text="暂无数据" :data="list">
        <el-table-column label="序号">
          <template v-slot="{$index}">
            {{ ($index + 1) + (queryParams.pageIndex - 1) * 10 }}
          </template>
        </el-table-column>
        <el-table-column prop="taskCode" label="工单编号" />
        <el-table-column prop="innerCode" label="设备编号" />
        <el-table-column prop="taskType.typeName" label="工单类型" />
        <el-table-column prop="createType" label="工单方式">
          <template v-slot="{row}">
            <span v-if="row.createType === 1">手动</span>
            <span v-else>自动</span>
          </template>
        </el-table-column>
        <el-table-column prop="taskStatus" label="工单状态">
          <template v-slot="{row}">
            <span v-if="row.taskStatus">{{ typeIdArr[row.taskStatus] }}</span>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="运维人员" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column prop="innerCode" label="操作" width="200px">
          <template v-slot="{row}">
            <el-button size="mini" type="text" @click="seeMoreInfo(row.taskId)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row v-if="totalPage > 1" align="middle" justify="space-between" style="height: 60px" type="flex">
        <span style="color: #dbdfe5">共{{ totalCount }}条记录  第 {{ queryParams.pageIndex }}/{{ totalPage }} 页</span>
        <el-pagination
          :current-page="queryParams.pageIndex"
          :page-size="queryParams.pageSize"
          :total="+total"
          layout="prev, next"
          next-text="下一页"
          prev-text="上一页"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog class="dialog" width="630px" title="工单详情" :visible.sync="showInfoDialog">
      <div class="dialog-wrapper">
        <div class="dialog-content">
          <img v-if="infoList.taskStatus === 3" src="../../assets/quxiao.png">
          <img v-if="infoList.taskStatus === 1" src="../../assets/daiban.png">
          <img v-if="infoList.taskStatus === 4" src="../../assets/wancheng.png">
          <div class="dialog-info">
            <div class="dialog-info-left">
              <p>设备编号：{{ infoList.innerCode }}</p>
              <p v-if="infoList.taskStatus === 3">取消日期：{{ infoList.updateTime }}</p>
              <p v-if="infoList.taskStatus === 4">完成日期：{{ infoList.updateTime }}</p>
              <p v-if="infoList.taskStatus === 1">运维人员：{{ infoList.userName }}</p>
              <p v-if="infoList.taskStatus === 1">工单方式：{{ infoList.createType === 1 ? '手动':'自动' }}</p>
              <p v-if="infoList.taskStatus === 3 || infoList.taskStatus === 4">工单类型：</p>
              <p v-if="infoList.taskStatus === 3">取消原因：{{ infoList.desc }}</p>
              <p v-if="infoList.taskStatus === 4">备注：{{ infoList.desc }}</p>
            </div>
            <div class="dialog-info-right">
              <p>创建日期：{{ infoList.createTime }}</p>
              <p v-if="infoList.taskStatus === 3 || infoList.taskStatus === 4">运维人员：{{ infoList.userName }}</p>
              <p v-if="infoList.taskStatus === 1">工单类型：</p>
              <p v-if="infoList.taskStatus === 3 || infoList.taskStatus === 4">工单方式：{{ infoList.createType === 1 ? '手动':'自动' }}</p>
              <p v-if="infoList.taskStatus === 1">备注：{{ infoList.desc }}</p>
              <p v-if="infoList.taskStatus === 4">定位：{{ infoList.addr }}</p>
            </div>
          </div>
        </div>
      </div>
      <div class="dialogbtn">
        <el-button v-show="infoList.taskStatus === 1" type="danger" @click="cancelTask">取消工单</el-button>
        <el-button v-show="infoList.taskStatus === 3" type="success" @click="addTask('nextadd')">重新创建</el-button>
      </div>
    </el-dialog>
    <el-dialog class="dialog" width="630px" title="新增工单" :visible.sync="showDialog">
      <el-form label-width="120px">
        <el-form-item label="设备编号">
          <el-input v-model="createFrom.innerCode" size="small" />
        </el-form-item>
        <el-form-item label="工单类型">
          <el-select v-model="createFrom.productType" style="width: 470px">
            <el-option label="投放工单" :value="1" />
            <el-option label="补货工单" :value="2" />
            <el-option label="维修工单" :value="3" />
            <el-option label="撤机工单" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="补货数量">
          <el-button type="text" @click="setShopList">
            <i class="el-icon-edit-outline" />
            补货清单</el-button>
        </el-form-item>
        <el-form-item label="运营人员">
          <el-select v-model="createFrom.userId" style="width: 470px">
            <el-option label="运营石凯安" :value="1" />
            <el-option label="运营秦翰墨" :value="6" />
            <el-option label="运营汪雨田" :value="7" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="createFrom.desc" type="textarea" size="small" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini">取消</el-button>
          <el-button size="mini" type="primary" @click="submit">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog width="630px" title="工单配置" :visible.sync="showConfigDialog">
      <el-form label-width="120px">
        <el-form-item label="补货警戒线">
          <el-input-number v-model="value" style="width: 440px" :min="1" :max="100" label="补货警戒线" controls-position="right" />
        </el-form-item>
        <div class="buhuoBtn">
          <el-button @click="showConfigDialog = false">取消</el-button>
          <button class="queren">确认</button>
        </div>
      </el-form>
    </el-dialog>
    <el-dialog width="630px" title="补货详情" :visible.sync="showShopList">
      <el-table :data="shopList" style="width: 100%" height="380">
        <el-table-column prop="channelCode" width="80" label="货道编号" />
        <el-table-column prop="sku.brandName" width="100" label="商品名称" />
        <el-table-column prop="currentCapacity" width="100" label="当前数量" />
        <el-table-column prop="maxCapacity" width="100" label="还可添加" />
        <el-table-column label="补满数量">
          <template v-slot="{row,$index}">
            <el-input-number v-model="shopNumList[$index]" :min="1" :max="row.maxCapacity" controls-position="right" />
          </template>
        </el-table-column>
      </el-table>
      <div class="buhuoBtn">
        <el-button @click="showShopList = false">取消</el-button>
        <button class="queren">确认</button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { typeIdArr } from '@/api/constants'
import Header from '@/components/Hander/index.vue'
import { getTasskList, getTaskInfo, createTask, getUserList, cancelTask } from '@/api/operation'
import { getChannelList } from '@/api/business'
export default {
  components: { Header },
  data() {
    return {
      shopNumList: [],
      showShopList: false,
      value: 60,
      list: [],
      typeId: [],
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        isRepair: false
      },
      createFrom: {
        assignorId: 10,
        createType: 1,
        innerCode: '01000023',
        productType: 3,
        userId: 1,
        desc: 'cnmd',
        details: []
      },
      infoList: [],
      showInfoDialog: false,
      showDialog: false,
      total: 0,
      totalPage: '',
      totalCount: '',
      status: null,
      taskCode: '',
      showConfigDialog: false,
      shopList: []
    }
  },
  async created() {
    const res = await getUserList({
      isRepair: true
    })
    console.log(res)
    this.getTasskList()
    this.typeIdArr = typeIdArr
  },
  methods: {
    async setShopList() {
      this.showShopList = true
      const res = await getChannelList(this.createFrom.innerCode)
      this.shopList = res
      this.shopNumList = new Array(this.shopList.length).fill(10)
      console.log(this.shopNumList)
    },
    configDialog() {
      this.showConfigDialog = true
    },
    cancelTask() {
      this.$confirm('取消工单后，将不能恢复，是否确认取消？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        showClose: false
      })
        .then(() => {
          cancelTask(this.infoList)
          this.showInfoDialog = false
          this.getTasskList(this.status, this.taskCode)
          // 在此处写上取消工单的代码
          this.$message({
            type: 'success',
            message: '取消工单成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    async searchList(status, orderId) {
      this.taskCode = orderId
      this.queryParams.pageIndex = 1
      this.status = status
      this.getTasskList(status, orderId)
    },
    async submit() {
      await createTask(this.createFrom)
      this.$message({
        type: 'success',
        message: '创建成功'
      })
      this.showDialog = false
    },
    async seeMoreInfo(id) {
      this.showInfoDialog = true
      const res = await getTaskInfo(id)
      this.infoList = res
      console.log(this.infoList)
    },
    addTask(type) {
      if (type === 'nextadd') {
        this.showInfoDialog = false
      }
      this.showDialog = true
    },
    async changePage(page) {
      this.queryParams.pageIndex = page
      if (this.status) {
        await this.getTasskList(this.status)
      } else {
        await this.getTasskList()
      }
    },
    async getTasskList(status, taskCode) {
      if (taskCode) {
        const res = await getTasskList({
          ...this.queryParams,
          taskCode
        })
        this.list = res.currentPageRecords
        this.total = res.totalCount
        this.totalPage = res.totalPage
        this.totalCount = res.totalCount
      } else if (status) {
        const res = await getTasskList({
          ...this.queryParams,
          status
        })
        this.list = res.currentPageRecords
        this.total = res.totalCount
        this.totalPage = res.totalPage
        this.totalCount = res.totalCount
      } else {
        const res = await getTasskList(this.queryParams)
        this.list = res.currentPageRecords
        this.total = res.totalCount
        this.totalPage = res.totalPage
        this.totalCount = res.totalCount
      }
    }
  }
}
</script>

<style scoped>
.main{
  width: 100%;
  height: 693px;
  padding: 20px;
  background-color: #fff;
  margin-bottom: 70px;
}
.btnBox {
  display: flex;
  padding-bottom: 10px;
}
.btn {
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  width: 80px ;
  height: 36px;
  padding: 0;
  background: linear-gradient(135deg, #ff9743, #ff5e20) ;
  border: none;
  color: white;
}
.configBtn{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 80px ;
  height: 36px;
  text-align: center;
  padding: 0;
  background-color: #f3e7e1;
}
::v-deep .el-pagination button {
width: 70px;
height: 32px;
margin: 0 16px;
border-radius: 2px;
background-color: #d5ddf8;
}

::v-deep .btn-prev:disabled {
background: #edf0f9;
}
::v-deep .dialog{
  border-radius: 30px;
}
.dialog-info{
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}
.dialogbtn{
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.dialog-content img{
  width: 600px;
}
.buhuoBtn{
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
.buhuoBtn .el-button{
  width: 80px;
  height: 36px;
  padding: 0;
  background-color: #fbf4f0 ;
  border: none;
  color: #655b56 ;
  font-size: 14px;
  border-radius: 4px;
}
.queren{
  width: 80px;
  height: 36px;
  padding: 0;
  background-color: #ff5e20 ;
  border: none;
  color: #fff ;
  font-size: 14px;
  border-radius: 4px;
  margin-left: 20px;
}
</style>
